package com.koron.order.maintain.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.maintain.bean.entity.MaintainReadyPo;
import com.koron.order.maintain.mapper.MaintainReadyMapper;
import com.koron.order.maintain.service.MaintainReadyService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (MaintainReady)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-14 14:12:53
 */
@Service("maintainReadyService")
public class MaintainReadyServiceImpl implements MaintainReadyService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MaintainReadyPo maintainReady) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);
        maintainReady.setId(CodeTools.getCode32());

        return mapper.insert(maintainReady);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MaintainReadyPo maintainReady) {
        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);
        return mapper.update(maintainReady);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public MaintainReadyPo queryById(SessionFactory factory, String id) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MaintainReadyPo> queryList(SessionFactory factory, MaintainReadyPo maintainReady) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);

        List<MaintainReadyPo> list = mapper.queryList(maintainReady);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<MaintainReadyPo> queryByOrderId(SessionFactory factory, String orderId) {

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);

        List<MaintainReadyPo> list = mapper.queryByOrderId(orderId);

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MaintainReadyPo> maintainReadyPoList) {

        if (CollectionUtils.isEmpty(maintainReadyPoList)) {
            return 0;
        }
        String maintainId = maintainReadyPoList.get(0).getMaintainId();


        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);

        List<MaintainReadyPo> poList = queryByOrderId(factory, maintainId);
        if(CollectionUtils.isNotEmpty(poList)){
            mapper.deleteByIds(poList.stream().map(MaintainReadyPo::getId).collect(Collectors.joining(",")));
        }
        maintainReadyPoList.forEach(p -> {
            p.setId(CodeTools.getCode32());
        });

        return mapper.insertBatch(maintainReadyPoList);
    }

    @Override
    @TaskAnnotation("updateBatch")
    public int updateBatch(SessionFactory factory, List<MaintainReadyPo> maintainReadyPoList) {

        if (CollectionUtils.isEmpty(maintainReadyPoList)) {
            return 0;
        }

        MaintainReadyMapper mapper = factory.getMapper(MaintainReadyMapper.class);

        return mapper.updateBatch(maintainReadyPoList);
    }

}